package com.d17_homework.homework3;

import java.lang.reflect.Field;

public class Demo3 {
    public static void main(String[] args) throws Exception {
        //有参构造创建学生类对象
        Student3 stu = new Student3("绝影",3);
        //调用getProperty方法
        Object name = getProperty(stu, "name");
        System.out.println(name);
    }

    private static Object getProperty(Object obj,String propertyName) throws Exception {
        //获取obj的字节码对象
        Class clazz = obj.getClass();
        //获取属性对象
        Field field = clazz.getDeclaredField(propertyName);
        field.setAccessible(true);//取消访问
        //获取属性值并返回
        Object o = field.get(obj);
        return o;
    }
}
